$(function () {
   // 定义事件处理
    document.onkeydown = function (event) {
        switch (event.key) {
            case 'a':
                bLeft = true;
                bRight=false;
                break;
            case 'w':
                bTop = true;
                bBottom=false;
                break;
            case 'd':
                bRight = true;
                bLeft=false;
                break;
            case 's':
                bBottom = true;
                bTop=false;
                break;
        }
    }
    document.onkeyup = function (event) {
        switch (event.key) {
            case 'a':
                bLeft = false;
                break;
            case 'w':
                bTop = false;
                break;
            case 'd':
                bRight = false;
                break;
            case 's':
                bBottom = false;
                break;
        }
    };
    window.onunload = function(){//窗口关闭之前
        var message=JSON.stringify({'state':'exit','userId':person[0].userId});
        websocket.send(message);
        websocket.close();
    }
    document.onmousedown=function (ev) {
        if(person[0].death==false){
            var oBullet=new Bullet(person[0].x,person[0].y,ev.pageX,ev.pageY,count,count);
            person[0].bullet.push(oBullet);
            var message=JSON.stringify({'state':'bullet','userId':person[0].userId,'x1':person[0].x,
                'y1':person[0].y,'x2':ev.pageX,'y2':ev.pageY,'width':count,'height':count});
            websocket.send(message);
        }
    }
    setInterval(function () {
        if(person[0]&&person[0].death==false){
            //移动事件
            if(bTop){
                person[0].moveUp();//移动后发送自己信息给其他人
                var message=JSON.stringify({'state':'move','userId':person[0].userId,'userName':person[0].userName,'x':person[0].x,'y':person[0].y});
                websocket.send(message);
            }
            if(bBottom){
                person[0].moveDown();
                var message=JSON.stringify({'state':'move','userId':person[0].userId,'userName':person[0].userName,'x':person[0].x,'y':person[0].y});
                websocket.send(message);
            }
            if(bLeft){
                person[0].moveLeft();
                var message=JSON.stringify({'state':'move','userId':person[0].userId,'userName':person[0].userName,'x':person[0].x,'y':person[0].y});
                websocket.send(message);
            }
            if(bRight){
                person[0].moveRight()
                var message=JSON.stringify({'state':'move','userId':person[0].userId,'userName':person[0].userName,'x':person[0].x,'y':person[0].y});
                websocket.send(message);
            }
        }
    },10);
    setInterval(function () {
        //刷新位置
        for(var i=0;i<person.length;i++){
            person[i].refreshPosition();
            person[i].hitTest(person,i);
            for (var j=0;j<person[i].bullet.length;j++){
                person[i].bullet[j].hitTest(person,i);
                person[i].bullet[j].refreshPosition(person[i]);
            }
        }
    },10);
});